package com.songshan.service;

import cn.hutool.core.bean.BeanUtil;
import com.songshan.bean.Menu;
import com.songshan.bean.User;
import com.songshan.dao.MenuMapper;
import com.songshan.dto.MenuNode;
import com.songshan.exception.BusinessException;
import com.songshan.filter.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MenuService {

  @Autowired
  private MenuMapper menuMapper;

  public List<MenuNode> menuTree(String mode) {
    List<MenuNode> menuNodes = new ArrayList<>();
    if (mode.equals("menu")) {
      MenuNode root = new MenuNode();
      root.setText(
          "<a onclick=\"addMenu()\" style=\"color:blue;text-decoration:underline\"><新建系统></a>");
      root.setIcon("");
      menuNodes.add(root);
    }
    List<Menu> list = menuMapper.selectAllMenu();
    if (list != null && !list.isEmpty()) {
      for (Menu menu : list) {
        if (mode.equals("main") && menu.getType().equals("功能")) {
          continue;
        }
        if (menu.getParentId() == null) {
          MenuNode parent = new MenuNode();
          BeanUtil.copyProperties(menu, parent);
          setChildNodes(parent, list, mode);
          parent.setNodeTags();
          menuNodes.add(parent);
        }
      }
    }
    return menuNodes;
  }

  private void setChildNodes(MenuNode parent, List<Menu> list, String mode) {
    List<MenuNode> childNodes = new ArrayList<>();
    for (Menu menu : list) {
      if (mode.equals("main") && menu.getType().equals("功能")) {
        continue;
      }
      if (menu.getParentId() != null && menu.getParentId() == parent.getId()) {
        MenuNode child = new MenuNode();
        BeanUtil.copyProperties(menu, child);
        child.setpId(parent.getId());
        setChildNodes(child, list, mode);
        child.setNodeTags();
        childNodes.add(child);
      }
    }
    parent.setNodes(childNodes);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void addMenu(Menu menu) throws BusinessException {
    Menu existsMenu = menuMapper.selectMenuByCode(menu.getCode());
    if (existsMenu != null) {
      throw new BusinessException(500,menu.getCode() + "已存在！");
    }
    menu.setCreateTime(new Date());
    menuMapper.insertMenu(menu);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void modMenu(Menu srcMenu) throws BusinessException {
    Menu menu = menuMapper.selectMenuById(srcMenu.getId());
    BeanUtil.copyProperties(srcMenu, menu);
    Menu existsMenu = menuMapper.selectMenuByCode(menu.getCode());
    if (existsMenu != null && !existsMenu.getId().equals(menu.getId())) {
      throw new BusinessException(500,menu.getCode() + "已存在！");
    }
    menu.setModifyTime(new Date());
    menuMapper.updateMenu(menu);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void delMenu(Integer id) throws BusinessException {
    delChildMenu(id);
    menuMapper.deleteMenuById(id);
  }

  private void delChildMenu(Integer parentId){
    List<Menu> childs = menuMapper.selectMenuByParentId(parentId);
    if(childs==null||childs.isEmpty()){
      return;
    }
    for(Menu child:childs){
      delChildMenu(child.getId());
    }
    menuMapper.deleteMenuByParentId(parentId);
  }
}
